package org.dromara.fms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.web.core.BaseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.fms.domain.vo.FmsBillTypeVo;
import org.dromara.fms.domain.bo.FmsBillTypeBo;
import org.dromara.fms.service.impl.FmsBillTypeServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 财务主
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fms/billType")
public class FmsBillTypeController extends BaseService {

    private final FmsBillTypeServiceImpl fmsBillTypeService;

    /**
     * 查询财务主列表
     */
    @SaCheckPermission("fms:billType:list")
    @GetMapping("/list")
    public TableDataInfo<FmsBillTypeVo> list(FmsBillTypeBo bo, PageQuery pageQuery) {
        return fmsBillTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出财务主列表
     */
    @SaCheckPermission("fms:billType:export")
    @Log(title = "财务主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FmsBillTypeBo bo, HttpServletResponse response) {
        List<FmsBillTypeVo> list = fmsBillTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "财务主", FmsBillTypeVo.class, response);
    }

    /**
     * 获取财务主详细信息
     *
     * @param typeId 主键
     */
    @SaCheckPermission("fms:billType:query")
    @GetMapping("/{typeId}")
    public R<FmsBillTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long typeId) {
        return R.ok(fmsBillTypeService.queryById(typeId));
    }

    /**
     * 新增财务主
     */
    @SaCheckPermission("fms:billType:add")
    @Log(title = "财务主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FmsBillTypeBo bo) {
        return toAjax(fmsBillTypeService.insertByBo(bo));
    }

    /**
     * 修改财务主
     */
    @SaCheckPermission("fms:billType:edit")
    @Log(title = "财务主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FmsBillTypeBo bo) {
        return toAjax(fmsBillTypeService.updateByBo(bo));
    }

    /**
     * 删除财务主
     *
     * @param typeIds 主键串
     */
    @SaCheckPermission("fms:billType:remove")
    @Log(title = "财务主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] typeIds) {
        return toAjax(fmsBillTypeService.deleteWithValidByIds(List.of(typeIds), true));
    }
}
